package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.GoodsDetailActivity;
import com.xgkj.diyiketang.data.GoodsInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class MallGoodsAdapter extends BaseAdapter {

    private Context mContext;

    private List<GoodsInfo> mList = new ArrayList<GoodsInfo>();

    public MallGoodsAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<GoodsInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.goodsadapter, parent, false);
            childHolder.goodsImage = (ImageView) convertView.findViewById(R.id.goods_img);
            childHolder.goodsName = (TextView) convertView.findViewById(R.id.goods_name);
            childHolder.goodsPrice = (TextView) convertView.findViewById(R.id.goods_price);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }



        final GoodsInfo info = mList.get(position);
        if (info != null) {
            final String videologo = info.getGoodsImage();
            if (videologo != null && videologo.length() > 0) {
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.goodsImage, videologo, new ViewUtil.Size(ViewUtil.dip2px(mContext,175),ViewUtil.dip2px(mContext,175)));

            }
            childHolder.goodsImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, GoodsDetailActivity.class);
                    intent.putExtra("ID",info.getId());
                    intent.putExtra("NAME",info.getGoodsName());
                    intent.putExtra("IMAGE",videologo);
                    intent.putExtra("PRICE",info.getGoodsPrice());
                    intent.putExtra("DESC",info.getDescribe());
                    mContext.startActivity(intent);
                }
            });
            childHolder.goodsName.setText(info.getGoodsName());
            childHolder.goodsPrice.setText(info.getGoodsPrice());
        }

        return convertView;
    }

    class ChildHolder {
        ImageView goodsImage;
        TextView goodsName;
        TextView goodsPrice;
    }
}
